<template>
    <div class='login_container'>
        <div class="login_box">
            <!-- 头像区域 -->
            <div class="avatar_box">
                <img src="../assets/logo.png" alt="">
            </div>
            <!-- 登录区域 -->
            <el-form :label-position="labelPosition" :rules="formLabelAlignRules" ref="loginRef" label-width="40px" :model="formLabelAlign" id='Login'>
              <el-form-item  id='uname' prop="username">
                <el-input v-model="formLabelAlign.username" prefix-icon="el-icon-user-solid" ></el-input>
              </el-form-item>
              <el-form-item  id='password' prop="password">
                <el-input v-model="formLabelAlign.password"  prefix-icon="el-icon-lock" type='password' ></el-input>
              </el-form-item>
            </el-form>
            <!-- 按钮区域 -->
            <el-row id='btn'>
                <el-button type="success" round id='login' @click="submitForm('loginRef')" >登录</el-button>
                <el-button type="info" round id='reset' @click='resetForm("loginRef")'>重置</el-button>
            </el-row>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      labelPosition: 'right',
      formLabelAlign: {
        username: 'admin',
        password: '123456'
      },
      formLabelAlignRules: {
        //   校验用户名
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }],
        //   校验密码
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm (nameForm) {
      this.$refs[nameForm].resetFields()
    },
    submitForm (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const data = await this.$http.post('login', this.formLabelAlign)
          if (data.meta.status === 200) {
            window.sessionStorage.setItem('token', data.data.token)
            this.$Message.success('登陆成功')
            this.$router.push('/home')
          } else {
            return this.$Message.error('登陆失败')
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
    .login_container {
        position: relative;
        height: 100%;
        background-color: #2b5b6b;
    }
    .login_box {
        width: 450px;
        height: 300px;
        background: #fff;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%)
    }
    .avatar_box {
        width: 130px;
        height: 130px;
        position: absolute;
        border:1px solid #ccc;
        border-radius: 50%;
        left:50%;
        transform: translate(-50%,-35%);
        box-shadow: 0 0 10px #ddd;
    }
    .avatar_box img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
    }
    #Login{
        width: 470px;
        position: absolute;
        top:35%
    }
    #btn {
        position: absolute;
        top:80%;
        left:50%
    }
    #uname {
        width: 90%;
    }
    #password {
        width: 90%;
    }
    #login{
        position: absolute;
        width: 80px;
    }
    #reset{
        position: absolute;
        width: 80px;
        transform: translateX(100%);
        text-align: center;
    }

</style>
